package com.supreme.uploadserver.service;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class HtmlParseService {

    public void stringParse() {
        String htmlStrBuilder =
                "<html>" +
                "<head>" +
                "<title>First parse</title>" +
                "<body>" +
                "<p id=basic>Parsed HTML into a doc.</p>" +
                "<p id=hello>Parsed HTML into a doc2.</p>" +
                "</body>" +
                "</html>";
//        String html = "<html><head><title>First parse</title></head>"
//                + "<body><p>Parsed HTML into a doc.</p><p id=hello>Parsed HTML into a doc2.</p></body></html>";
        Document document = Jsoup.parse(htmlStrBuilder);
        System.out.println(document);

        final Element hello = document.getElementById("hello");
        Optional.ofNullable(hello).ifPresent(Node::remove);

        final Element basic = document.getElementById("basic");
        if (basic != null) {
            System.out.println(basic.text());
            basic.prepend("<div>item0</div>");
            basic.append("<div>item1</div>");
        }

        System.out.println(document);
    }
}
